import { reqUserLogin } from '@/api/user'
import { message } from '@/components/Message'

// 用户状态
export default {
  namespaced: true,
  state: () => {
    return {
      profile: {
        id: '',
        nickname: '',
        token: ''
      }
    }
  },
  mutations: {
    setUserId (state) {
      state.profile.id = 10001
    },
    setProfile (state, profile) {
      state.profile = profile
    }
  },
  actions: {
    getProfile ({ commit }, data) {
      // eslint-disable-next-line no-async-promise-executor
      return new Promise(async (resolve, reject) => {
        const res = await reqUserLogin(data).catch(e => e)
        // 判断是否账号密码错误
        // eslint-disable-next-line eqeqeq
        if ((res.response && res.response.status == 400) || res.message === "Cannot read properties of undefined (reading 'token')") {
          return message({ type: 'error', text: '用户名密码错误！' })
        }
        if (res) {
          commit('setProfile', res.result)
          resolve(res)
        } else {
          reject(res)
        }
      })
    }
  }
}
